package com.company.project.configurer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.company.project.constant.MQEnums;
import com.company.project.rocketmq.AceMQConsumer;
import com.company.project.rocketmq.AceMQProducer;
import com.company.project.rocketmq.IMessageProcessor;
import com.company.project.rocketmq.processor.CouponCancelOrderProcessor;
import com.company.project.rocketmq.processor.GoodsCancelOrderProcessor;
import com.company.project.rocketmq.processor.OrderCancelOrderProcessor;
import com.company.project.rocketmq.processor.PaidProcessor;
import com.company.project.rocketmq.processor.UserCancelOrderProcessor;

@Configuration
public class RocketmqConfigurer {
	@Autowired
	OrderCancelOrderProcessor orderCancelOrderProcessor;
	@Autowired
	UserCancelOrderProcessor userCancelOrderProcessor;
	@Autowired
	CouponCancelOrderProcessor couponCancelOrderProcessor;
	@Autowired
	GoodsCancelOrderProcessor goodsCancelOrderProcessor;
	@Autowired
	PaidProcessor paidProcessor;

	/**
	 * user 模块 消费者启动
	 */
	@Bean(initMethod = "init")
	public AceMQConsumer userInitAceMQConsumer() throws Exception {
		String groupName = "user_ace_trade_cancel_group";
		return this.creatConsumerBean(groupName, userCancelOrderProcessor);
	}

	/**
	 * coupon 优惠券模块 消费者启动
	 */
	@Bean(initMethod = "init")
	public AceMQConsumer couponInitAceMQConsumer() throws Exception {
		String groupName = "coupon_ace_trade_cancel_group";
		// IMessageProcessor processor = new CouponCancelOrderProcessor();
		return this.creatConsumerBean(groupName, couponCancelOrderProcessor);
	}

	/**
	 * order 模块 消费者启动
	 */
	@Bean(initMethod = "init")
	public AceMQConsumer orderInitAceMQConsumer() throws Exception {
		String groupName = "order_ace_trade_cancel_group";
		// IMessageProcessor processor = new OrderCancelOrderProcessor();
		return this.creatConsumerBean(groupName, orderCancelOrderProcessor);
	}

	/**
	 * goods 模块 消费者启动
	 */
	@Bean(initMethod = "init")
	public AceMQConsumer goodsInitAceMQConsumer() throws Exception {
		String groupName = "goods_ace_trade_cancel_group";
		// IMessageProcessor processor = new GoodsCancelOrderProcessor();
		return this.creatConsumerBean(groupName, goodsCancelOrderProcessor);
	}

	public AceMQConsumer creatConsumerBean(String groupName, IMessageProcessor processor) {
		AceMQConsumer bean = new AceMQConsumer();
		bean.setGroupName(groupName);
		bean.setNamesrvAddr("192.168.175.130:9876");
		bean.setTags(MQEnums.TopicEnum.ORDER_CANCEL.getTag());
		bean.setProcessor(processor);
		bean.setTopic(MQEnums.TopicEnum.ORDER_CANCEL.getTopic());
		return bean;
	}

	/**
	 * Order 模块 收到支付成功的消息 更改订单状态
	 */
	@Bean(initMethod = "init")
	public AceMQConsumer changeOrderStatusAceMQConsumer() throws Exception {
		String groupName = "goods_ace_trade_change_group";
		// IMessageProcessor processor = new PaidProcessor();
		AceMQConsumer bean = new AceMQConsumer();
		bean.setGroupName(groupName);
		bean.setNamesrvAddr("192.168.175.130:9876");
		bean.setTags(MQEnums.TopicEnum.PAY_PAID.getTag());
		bean.setProcessor(paidProcessor);
		bean.setTopic(MQEnums.TopicEnum.PAY_PAID.getTopic());
		return bean;
	}

	/**
	 * 生产者 1. 下单失败时，发送取消订单的消息 2. 支付回调成功时 发送消息 删除消息临时表里面的 记录
	 *
	 */
	@Bean(initMethod = "init")
	public AceMQProducer initAceMQProducer() throws Exception {
		AceMQProducer bean = new AceMQProducer();
		bean.setGroupName("AceProducerGroup");
		bean.setNamesrvAddr("192.168.175.130:9876");
		return bean;
	}

}
